package com.fishing.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fishing.util.personal.MyHashMap;

import QL.ButlerGameGetRoomConfigRequest;
import QL.ButlerGameGetRoomConfigResponse;
import QL.ButlerGameGetRoomPlatformDetailRequest;
import QL.ButlerGameGetRoomPlatformDetailResponse;
import QL.ButlerGameGetRoomPlatformSummaryRequest;
import QL.ButlerGameGetRoomPlatformSummaryResponse;
import QL.ButlerGameGetRoomServersRequest;
import QL.ButlerGameGetRoomServersResponse;
import QL.ButlerGameRoomPlatformDetail;
import QL.ButlerGameSetRoomPlatformDifficultyRequest;
import QL.ButlerGameSetRoomPlatformDifficultyResponse;
import QL.ButlerGameSetRoomPlatformParamRequest;
import QL.ButlerGameSetRoomPlatformParamResponse;
import QL.QLClient;

/**
 * 房间控制-捕鱼控制
 * @author lrz
 * */
@Controller
public class RoomFishController {
	private static QLClient  client = new QLClient();
    
	//region @RequestMapping("{roomId}"),加不加斜杠都可以解析出来
	@RequestMapping("room/fish")
	public ModelAndView fish(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_fish_3D");
		return mav;
	}
	//endregion
	
	//region 进入捕鱼2D房间
	@RequestMapping("room/fish/two")
	public ModelAndView two(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_fish_2D");
		return mav;
	}
	//endregion
	
	//region进入竖版捕鱼房间
	@RequestMapping("room/sbby")
	public ModelAndView sbby(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_sbby");
		return mav;
	}
	//endregion
	
	//region进入超凡捕鱼房间
	@RequestMapping("room/cfby")
	public ModelAndView cfby(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_fish_cfby");
		return mav;
	}
	//endregion
	
	//region进入深海捕鱼房间
	@RequestMapping("room/shby")
	public ModelAndView hhby(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_fish_shby");
		return mav;
	}
	//endregion

	//region进入集结号捕鱼房间
	@RequestMapping("room/jjh")
	public ModelAndView jjh(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_fish_jjh");
		return mav;
	}
	//endregion
	
	//region进入水果玛丽房间
	@RequestMapping("room/sgml")
	public ModelAndView sgml(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_sgml");
		return mav;
	}
	//endregion
	
	//region进入性感女郎房间
	@RequestMapping("room/xgny")
	public ModelAndView xgny(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_xgny");
		return mav;
	}
	//endregion

	//region进入百家乐房间
	@RequestMapping("room/bjl")
	public ModelAndView bjl(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_bjl");
		return mav;
	}
	//endregion

	//region进入炸金花 房间
	@RequestMapping("room/zjh")
	public ModelAndView zjh(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/config_zjh");
		return mav;
	}
	//endregion
	
	/**
	 * 读取 服务器 房间 台子配置
	 * @param ServerId 服务器
	 * @param ConfigId 房间
	 * @param PlatformId 台子
	 * @param GameId 游戏编号
	 * @param SiteId 玩法编号
	 * @return Map
	 * */
	@ResponseBody
	@RequestMapping(value="room/fish/table",produces="text/html;charset=UTF8")
	public Map table(String ServerId,String ConfigId,String GameId,String SiteId,String PlatformId,HttpServletRequest request){
		long AllPlayersGain = 0;
		long AllPlayersCost = 0;
		long DifficultAllLevel = 0;
		int Difficult = 0;
		//读取某个服务器有几个房间,几个台子,和具体台子的水位线
		ButlerGameGetRoomPlatformDetailRequest req = new ButlerGameGetRoomPlatformDetailRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		req.GameId = Integer.parseInt(GameId); //游戏编号
		req.SiteId = Integer.parseInt(SiteId); //玩法编号 1是3D捕鱼 2是2D捕鱼
		req.ServerId = Integer.parseInt(ServerId); //捕鱼服务器
		req.ConfigId = Integer.parseInt(ConfigId);//代表房间ID
		req.PlatformId = -1; //代表所有台子
		ButlerGameGetRoomPlatformDetailResponse rsp = new ButlerGameGetRoomPlatformDetailResponse();
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		
		//读取每个房间设置的水位线
		ButlerGameGetRoomConfigRequest req2 = new  ButlerGameGetRoomConfigRequest();
		req2.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		req2.GameId = 1;
		req2.SiteId = 1;
		ButlerGameGetRoomConfigResponse rsp2 = new ButlerGameGetRoomConfigResponse();
		client.Execute(req2, rsp2);
		
		
		if(rsp2.ErrMsg == "" || rsp2.ErrMsg == null) {
			//得到对应房间的水位设置,来与当前水位比较,来显示当前水位的难度
			String[] difficult = rsp2.ConfigList.get((int) req.ConfigId - 1).DifficultyBoundary.split(",");
			//得到当前台子总共有多少难度级别
			List<String> difficultAllLevelList = Arrays.asList(difficult);
			List list = new ArrayList();
			
			
			for(int i = 0,size = rsp.DetailList.size();i < size;i++) {
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("PlatformId", rsp.DetailList.get(i).PlatformId);//当前台子ID
				map.put("WaterLine", rsp.DetailList.get(i).WaterLine);//当前台子水位线
				map.put("FeeGrab", rsp.DetailList.get(i).FeeGrab);//当前台子总税收
				map.put("DifficultAllLevel", difficultAllLevelList.size() + 1);//当前台子共有多少难度级别
				map.put("WaterLineLimit", difficult);//当前台子水位线界限
				//所有服务器所有台子总获得 
				AllPlayersGain += rsp.DetailList.get(i).PlayersGain;
				//所有服务器所有台子总消耗
				AllPlayersCost +=  rsp.DetailList.get(i).PlayersCost;
				
				//得到当前水位线对应的难度级别
				int j = 0;
				for(j = 0;j < difficultAllLevelList.size();j++) {
					if((rsp.DetailList.get(i).WaterLine <= Long.parseLong(difficultAllLevelList.get(j)) && j == 0) ||(rsp.DetailList.get(i).WaterLine <= Long.parseLong(difficultAllLevelList.get(j)) && j > 0)) {
						map.put("DifficultLevel", (j+1) + "级");
						break;
					}
				}
				if(j == difficultAllLevelList.size()) {
					map.put("DifficultLevel", (j+1) + "级");
				}
				list.add(map);
			}
			//System.out.println(JSONArray.toJSON(list));
			
			
			
			/*//只要玩家总消耗和玩家总获得
			Map<String,Object> map_data = new HashMap<String,Object>();
			map_data.put("AllPlayersGain", AllPlayersGain);
			map_data.put("AllPlayersCost", AllPlayersCost);
			map_data.put("DifficultAllLevel", difficultAllLevelList.size()+1);
			//清除上面对每一个台子统计的数据
			list.clear();
			
			//添加界限
			String arr = rsp2.ConfigList.get(0).DifficultyBoundary;
			map_data.put("WaterLineLevel", JSONArray.toJSON(arr));
			//提取 总水位线 和 总固定资产
			ButlerGameGetRoomPlatformSummaryRequest req3 = new ButlerGameGetRoomPlatformSummaryRequest();
			ButlerGameGetRoomPlatformSummaryResponse rsp3 = new ButlerGameGetRoomPlatformSummaryResponse();
			long AllWaterLine = 0;
			long AllFeeGrab = 0;
			req3.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
			req3.GameId = Integer.parseInt(GameId); //游戏编号
			req3.SiteId = Integer.parseInt(SiteId); //玩法编号 1是3D捕鱼 2是2D捕鱼
			req3.ServerId = Integer.parseInt(ServerId); //捕鱼服务器
			req3.ConfigId = Integer.parseInt(ConfigId);//代表房间ID
			client.Execute(req3, rsp3);
			for(int i = 0;i < rsp3.SummaryList.size();i++) {
				AllWaterLine += rsp3.SummaryList.get(i).WaterLine;
				AllFeeGrab += rsp3.SummaryList.get(i).FeeGrab;
			}
			map_data.put("AllWaterLine", AllWaterLine);
			map_data.put("AllFeeGrab", AllFeeGrab);
			//增加所有台子合计的数据
			//当前房间的在线玩家个数
			map_data.put("OnlineUserCount", rsp.OnlineUserCount);
			list.add(map_data);
			Map<String,Object> mapdata = new HashMap();
			mapdata.put("status", "200");
			mapdata.put("message", "");
			mapdata.put("count", list.size());
			mapdata.put("data", JSONArray.toJSON(list));
			System.out.println(JSONArray.toJSON(list));
			return mapdata;*/
			Map<String,Object> mapdata = new HashMap();
			mapdata.put("status", "200");
			mapdata.put("message", "");
			mapdata.put("count", list.size());
			mapdata.put("data", list);
			return mapdata;
		}else {
			Map<String,Object> mapdata = new HashMap();
			mapdata.put("status", "200");
			mapdata.put("message", "");
			mapdata.put("count", 0);
			mapdata.put("data", "");
			return mapdata;
		}
		
	}
	
	@ResponseBody
	@RequestMapping(value="room/fish/ServerId",produces="text/html;charset=UTF8")
	public Object ServerId(String Game_SiteId,HttpServletRequest request) {
		ButlerGameGetRoomServersRequest req = new ButlerGameGetRoomServersRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		if(Game_SiteId.equals("1")) {
			req.GameId = 1;
			req.SiteId = 1;
		}
		if(Game_SiteId.equals("2")) {
			req.GameId = 1;
			req.SiteId = 2;
		}
		ButlerGameGetRoomServersResponse rsp = new ButlerGameGetRoomServersResponse();
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		String serverId = rsp.ServerIds;
		return JSONArray.toJSON(serverId);
	}
	
	/***
	 * 设置台子难度
	 * @param GameId      游戏
	 * @param SiteId      玩法
	 * @param ServerId    服务器
	 * @param ConfigId    房间
	 * @param PlatformId  台子
	 * @param setLevel    设置等级
	 * */
	@ResponseBody
	@RequestMapping(value="room/fish/setDifficult",produces="text/html;charset=UTF8")
	public String setDifficult(HttpServletRequest request,String GameId,String SiteId,String ServerId,String ConfigId,String PlatformId,String setLevel) {
		try {
			ButlerGameSetRoomPlatformDifficultyRequest req = new ButlerGameSetRoomPlatformDifficultyRequest();
			req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
			req.GameId = Integer.parseInt(GameId);
			req.SiteId = Integer.parseInt(SiteId);
			req.ServerId = Integer.parseInt(ServerId);
			req.ConfigId = Integer.parseInt(ConfigId);
			req.PlatformId = Integer.parseInt(PlatformId);
			req.DifficultyLevel = Integer.parseInt(setLevel);
			req.Operator = (String)request.getSession().getAttribute("Admintruename");
			ButlerGameSetRoomPlatformDifficultyResponse rsp = new ButlerGameSetRoomPlatformDifficultyResponse();
			if (req.ZoneId < 0)
				client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
			client.Execute(req, rsp);
		} catch (Exception e) {
			return "fail";
		}
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value="room/SetRoom",produces="text/html;charset=UTF8")
	public String SetRoom(HttpServletRequest request,String GameId,String SiteId,String ServerId,String ConfigId,String PlatformId,String Delta,String Action) {
		try {
			ButlerGameSetRoomPlatformParamRequest req = new ButlerGameSetRoomPlatformParamRequest();
			req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
			req.GameId = Integer.parseInt(GameId);
			req.SiteId = Integer.parseInt(SiteId);
			req.ServerId = 0;
			req.ConfigId = Integer.parseInt(ConfigId);
			if(Integer.parseInt(GameId) != 1 && Integer.parseInt(GameId) != 4)
				req.PlatformId = -1;
			else
				req.PlatformId = Integer.parseInt(PlatformId);
			req.Action = Integer.parseInt(Action);
			req.Delta = Integer.parseInt(Delta);
			req.Operator = (String)request.getSession().getAttribute("Admintruename");
			ButlerGameSetRoomPlatformParamResponse rsp = new ButlerGameSetRoomPlatformParamResponse();
			if (req.ZoneId < 0)
				client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
			client.Execute(req, rsp);
			if(rsp.ErrMsg != null) {
				return rsp.ErrMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
		return "success";
	}
	
	@RequestMapping("room/toSetRoom")
	public ModelAndView toSetRoom(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("room/SetRoom");
		return mav;
	}
}
















